package com.mt.fpb.controller;

import com.vbao.service.trade.VbaoChargeSystemHandler;
import com.vbao.service.trade.bean.request.QRCodeRefundRequest;
import com.vbao.service.trade.bean.request.TradeRequestXml;
import com.vbao.service.trade.utils.Base64;
import com.vbao.service.trade.utils.TradeCodeEnum;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class VbaoReFoundController {

    public static void main(String[] args) {
        String InputCharset = "UTF-8";
        String customerNo = "202108240002";
        String key = "n7hPATGKB6aQRfmncrCV";
        String goodNo = "ONEQRCODE";
        String requestGateWayRefundPayXmlurl = "http://pay.chinavbao.com/vbaoPay/cust/payService/QRCodeRefund";
        QRCodeRefundRequest qrCodeRefundRequest = new QRCodeRefundRequest();

        qrCodeRefundRequest.setVersion("6.0");
        qrCodeRefundRequest.setSignType("MD5");
        qrCodeRefundRequest.setInputCharset(InputCharset);
        qrCodeRefundRequest.setCustomerNo(customerNo);
        qrCodeRefundRequest.setGoodNo(goodNo);
        //"YMF" + System.nanoTime()
        qrCodeRefundRequest.setOrderNo("12121211200"); //退款订单号
        qrCodeRefundRequest.setPayDate("20210906"); //支付时间，格式YYYYMMDD
        qrCodeRefundRequest.setRefundOrderNo("1422802410253873154"); //被退款的订单号，原支付的商户订单号
        qrCodeRefundRequest.setRefundAmount("2000");  //退款金额，单位元  格式：100.00
        //http://4130y2w273.qicp.vip   http://money.confidant.top
        qrCodeRefundRequest.setNotifyUrl("http://4130y2w273.qicp.vip:9595/getVbao/get");
        qrCodeRefundRequest.setExt1("1");
        qrCodeRefundRequest.setExt2("优享宝-90");
        VbaoChargeSystemHandler vbaoChargeSystemHandler = new VbaoChargeSystemHandler();
        TradeRequestXml tradeRequestXml = vbaoChargeSystemHandler.creatRequestXml(qrCodeRefundRequest, key, InputCharset, TradeCodeEnum.QRCODE_PAY_REFUND.getCode());
        System.out.println("发送明文报文:" + tradeRequestXml.getOriRequestXml());
        System.out.println("发送密文报文:" + tradeRequestXml.getRequestXml());
        if (tradeRequestXml.getErrMsg() == null) {
            String returnDKPayxml = vbaoChargeSystemHandler.http(requestGateWayRefundPayXmlurl, tradeRequestXml.getRequestXml(), InputCharset);
            System.out.println("返回密文报文" + returnDKPayxml);
            if (null != returnDKPayxml && returnDKPayxml.length() > 0) {
                String returnOriDKPayxml = "";

                try {
                    returnOriDKPayxml = Base64.decode(returnDKPayxml, InputCharset);
                    System.out.println("解密后报文" + returnOriDKPayxml);
                    if (null != returnOriDKPayxml && returnOriDKPayxml.length() > 0) {
                        boolean flag = vbaoChargeSystemHandler.verifySignature(returnOriDKPayxml, key, InputCharset);
                        if (flag) {
                            System.out.println("验签正确");
                            System.out.println(URLDecoder.decode(returnOriDKPayxml, InputCharset));
                        }
                    }
                } catch (UnsupportedEncodingException var12) {
                    System.out.println("解析密文报文有误:" + var12.getMessage());
                }
            }
        } else {
            System.out.println(tradeRequestXml.getErrMsg());
        }

    }
}
